﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Tour.BLL;
using Tour.DAL;

public partial class Admin_Tour_TourTypeList : VST.Controls.BaseLCGPage
{
    public int? ParentId { get; set; }
    public bool? IsVisible { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDataParentDDL();
            GetPropertyFromQueryString();
            LoadData();
        }
    }
    void GetPropertyFromQueryString()
    {
        ParentId = CommonHelper.QueryStringInt("ParentId", -1);
        IsVisible = CommonHelper.QueryStringBool("IsVisible");
        ddlParent.SelectedValue = ParentId.ToString();
        chbVisible.Checked = IsVisible.Value;
    }
    void GetPropertyFromForm()
    {
        ParentId = ddlParent.SelectedValue.ToInt();
        IsVisible = chbVisible.Checked;
    }
    void LoadDataParentDDL()
    {
        ddlParent.DataSource = TblTourTypeManager.CreateInstant().SelectTblTourTypeForDDL(-1, WorkingLanguage);
        ddlParent.DataTextField = "Name";
        ddlParent.DataValueField = "ID";
        ddlParent.DataBind();
    }
    void LoadData()
    {
        grv.DataSource = TblTourTypeManager.CreateInstant().SelectTblTourTypeBy(null, ParentId, WorkingLanguage, "", IsVisible, "Id", "DESC");
        grv.DataBind();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        GetPropertyFromForm();
        string Url = string.Format("TourTypeList.aspx?ParentId={0}&IsVisible={1}", ParentId, IsVisible);
        //string Url = string.Format("TblChannelList.aspx?ParentID={0}", ParentID);
        Response.Redirect(Url);
    }
    protected void grv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        var hdfID = (HiddenField)grv.Rows[e.RowIndex].FindControl("hdfID");
        var hdfParentId = (HiddenField)grv.Rows[e.RowIndex].FindControl("hdfParentID");
        try
        {
            UpdateOrderOffAndDelete(hdfID.Value, hdfParentId.Value, 100);
            TblTourTypeManager.CreateInstant().Delete(hdfID.Value.ToInt());
            Response.Redirect(Request.RawUrl);
            Trace.Warn("Delete Success" + hdfID.Value);
        }
        catch (Exception ex)
        {
            Trace.Warn("Delete Error" + hdfID.Value + ex.Message);
        }
        LoadData();
        Response.Redirect(Request.RawUrl);
    }
    protected void btnUpdateOrder_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < grv.Rows.Count; i++)
        {
            var txtOrder = (TextBox)grv.Rows[i].FindControl("txtOrder");
            var hdfOrder = (HiddenField)grv.Rows[i].FindControl("hdfOrder");
            var hdfParentId = (HiddenField)grv.Rows[i].FindControl("hdfParentId");
            var hdfID = (HiddenField)grv.Rows[i].FindControl("hdfID");
            int OldIndex = int.Parse(hdfOrder.Value);
            int NewIndex = int.Parse(txtOrder.Text);
            if (OldIndex != NewIndex)
                UpdateOrderOffAndDelete(hdfID.Value, hdfParentId.Value, NewIndex);
        }
        Response.Redirect(Request.RawUrl);
    }
    private void UpdateOrderOffAndDelete(string strId, string strParentId, int NewIndex)
    {
        string Whereclause = " And ParentId IS NULL ";
        if (!strParentId.Equals(""))
        {
            Whereclause = String.Format(" And ParentId={0}", strParentId);
        }
        string Whereclause1 = String.Format(" Id={0}", strId);
        string Whereclause2 = String.Format(" And Id<>{0}", strId);
        Tools.SqlHelper.UpdateOrders("TblTourType", Whereclause, Whereclause1, Whereclause2, NewIndex);
    }
    protected void grv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {//
        GetPropertyFromForm();
        grv.PageIndex = e.NewPageIndex;
        LoadData();
    }
}